#!/usr/bin/env python
# -*- coding:utf-8 -*-

'''

mp3para.js
var mp3url = 'http://file2.rrting.net/allfile598/english/ppyy/meiwen/5542/bcbdc23853490cdc3b0824bb2ab8dc9f.mp3';
var texturl ='http://file2.rrting.net/allfile598/english/ppyy/meiwen/5542/bcbdc23853490cdc3b0824bb2ab8dc9f.lrc';
var wordurl ='/English/zwmzxl/264503/264503.txt';

'''

import hashlib
import urllib.request
from lxml import html
from lxml import etree
import sys

#--------------------------------------------------------------------------------
# 文件工具

import os
CODING = 'UTF8'

def yy_file_read_bytes(filename):
    assert(os.path.isfile(filename)), filename
    return open(filename, 'rb').read()

def yy_file_read_content(filename, coding=CODING):
    assert(os.path.isfile(filename)), filename
    data = yy_file_read_bytes(filename)
    return data.decode(coding)

def yy_file_write_bytes(filename, data):
    fp = open(filename, 'wb')
    fp.write(data)
    fp.close()

def yy_file_write_content(filename, content, coding=CODING):
    yy_file_write_bytes(filename, content.encode(coding))


def cacl_md5(item):
    if isinstance(item, str):
        item = item.encode()
    assert(isinstance(item, bytes))
    m2 = hashlib.md5()
    m2.update(item)
    return m2.hexdigest()


#--------------------------------------------------------------------------------
# html工具

def url_read_data(url) -> bytes:
    cache_name = download_cache_dir + url.split('//')[-1].replace('/', '_') + '.htm'
    if os.path.isfile(cache_name):
        data = yy_file_read_bytes(cache_name)
    else:
        data = urllib.request.urlopen(url)  #urlopen用来打开一个网页
        data = data.read()   #这里的rend()是必须的，否则不能打印源码。
        yy_file_write_bytes(cache_name, data)
    return data

def url_read(url, coding = 'utf8') -> str:
    data = url_read_data(url)
    data = data.replace(b'\xaa\xb2',b'').replace(b'\xaa\xb1',b'').decode(coding)
    return data

def url_get_last_name(url:str):
    return(url.rsplit('/', 1)[-1])

def lxml_root_from_url(url, coding = 'utf8') -> etree.Element:
    data = url_read(url, coding)
    root_el = html.fromstring(data)
    return root_el

def lxml_el_text_content(el) -> str:
    return el.text_content()


#--------------------------------------------------------------------------------

def rrting_get_url_string(subpath):
    if subpath.lstrip()[:7].lower() == 'http://':
        return subpath
    return 'http://www.rrting.net%s' % subpath

def rrting_find_100_english_classics_recite(url, coding = 'utf8'):  # 英语经典美文诵读100篇汇总
    root_el = lxml_root_from_url(url, coding)
    results = []
    for el in root_el.xpath('//tr/td/a[@href and @target]'):
        results.append((lxml_el_text_content(el), rrting_get_url_string(el.get('href'))))
    return results


def rrting_parse_download_item(item:str) -> list:
    item = item.strip().replace(';', '').replace('var ', '').replace("'", '').replace("'", '')
    parts = item.split('=')
    return [i.strip() for i in parts]

def rrting_get_file_title_impl(title: str) -> str:
    #英语经典美文诵读100篇：001.life is to be whole -	MP3音频下载
    title = title.strip()
    parts = title.split('：')
    assert (len(parts) == 2), title
    parts = parts[1].split('-')
    #assert (len(parts) == 2), title
    return parts[0].strip().replace(' ', '_')

def rrting_get_file_title(root_el: html.HtmlElement) -> str:
    title = lxml_el_text_content(root_el.xpath('//div[@id="N_title"]/h1')[0])
    return rrting_get_file_title_impl(title)


def rrting_find_100_english_classics_recite_download(url, coding = 'utf8'):
    root_el = lxml_root_from_url(url, coding)
    assert (isinstance(root_el, html.HtmlElement))
    
    title = rrting_get_file_title(root_el)
    recite_files = [title]
    
    for el in root_el.xpath("//script[@src='mp3para.js']"):
        mp3para_js_url = '%s/%s' % (url, el.get('src'))
        url_content = url_read(mp3para_js_url, coding)
        
        for item in url_content.split('\n'):
            item = rrting_parse_download_item(item)
            item = item[1]
            recite_files.append(url_get_last_name(item))
            print(rrting_get_url_string(item))
        return recite_files
    return 


def html_parse(data):
    data = data.replace(' charset=gb2312"', '"')
    root_el = html.fromstring(data)
    for el in root_el.xpath('//tr/td/a[@href and @target]'):
        print(el.text_content(), el.attrib)
    #print(root)




if __name__=='__main__':
    #downloads_href = yy_file_read_content('rrting_100_classic.txt')
    download_cache_dir = r'F:/Test/cache/'
    coding = 'gb2312'
    url = rrting_get_url_string('/English/zwmzxl/264526')
    recites_files = []
    for name, suburl in rrting_find_100_english_classics_recite(url, coding):
        #print(name)
        suburl = rrting_get_url_string(suburl)
        item = rrting_find_100_english_classics_recite_download(suburl, coding)
        #print(item)
        recites_files.append(item)
    if 0:
        data_dir = r'E:/360/经典英语'
        for recite_files in recites_files:
            if recite_files:
                title = recite_files[0]
                for file in recite_files[1:]:
                    file = os.path.join(data_dir, file)
                    if os.path.isfile(file):
                        print('重命名：%s -> %s' % (file, os.path.join(data_dir, title + os.path.splitext(file)[-1])))
                        os.rename(file, os.path.join(data_dir, title + os.path.splitext(file)[-1]))
    #url = rrting_get_url_string('/English/zwmzxl/264503')
    #url = rrting_get_url_string('http://www.rrting.net/English/zwmzxl/264482/download.html')
    #rrting_find_100_english_classics_recite_download(url, coding)
    #res = rrting_find_100_english_classics_recite(url, 'gb2312')
    #html_parse(res)
    
    #url = rrting_get_url_string('http://file2.rrting.net/allfile598/english/ppyy/meiwen/5542/bcbdc23853490cdc3b0824bb2ab8dc9f.mp3')
    #print(url_read_data(url))